package controller

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"project/pkg/app"
	"project/pkg/e"
	"project/services"
)

func Login(c *gin.Context) {
	var m map[string]string
	b, _ := c.GetRawData()
	_ = json.Unmarshal(b, &m)
	username := m["username"]
	upassword := m["u_password"]

	err, userData, _ := services.Get_Users(username)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	if len(userData) < 1 {
		app.INFO(c, 30000, "没有此用户")
		return
	}
	if userData[0].Upassword != upassword {
		app.INFO(c, 30001, "密码错误")
		return
	}
	app.OK(c, map[string]interface{}{"users": userData[0]}, "登录成功")
}
